﻿#region License

// Copyright (c) 2011, Matt Holmes
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are permitted provided 
// that the following conditions are met:
// 
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the 
//   following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
//   the following disclaimer in the documentation and/or other materials provided with the distribution.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
// OF THE POSSIBILITY OF SUCH DAMAGE.

#endregion

using System;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using SkyrimTweaker.Common;
using SkyrimTweaker.Common.Services;
using SkyrimTweaker.Properties;

namespace SkyrimTweaker {
	public interface IShellViewModel {
		void Loaded ();
	}

	public class ShellViewModel : IShellViewModel {
		#region Fields

		private readonly IUnityContainer _container;
		private readonly IRegionManager _regionManager;
		private readonly IUpdateService _updateService;

		#endregion

		#region Constructors

		public ShellViewModel (IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator, IUpdateService updateService) {
			_container = container;
			_regionManager = regionManager;
			_updateService = updateService;
			eventAggregator.GetEvent<CloseApplicationEvent> ().Subscribe (() => Application.Current.MainWindow.Close (), ThreadOption.UIThread);
			eventAggregator.GetEvent<SelectedThemChangedEvent> ().Subscribe (s => {
				Settings.Default.CurrentTheme = s;
				Settings.Default.Save ();
			});

			eventAggregator.GetEvent<UpdateFoundEvent> ().Subscribe (OnUpdateFound, ThreadOption.UIThread);
			eventAggregator.GetEvent<UpdateCompleteEvent> ().Subscribe (OnUpdateDownloadComplete, ThreadOption.UIThread);
		}

		#endregion

		#region Private Methods

		private void OnUpdateDownloadComplete (string file) {
			MessageBox.Show ("Update download is complete. Restarting SkyrimTweaker to apply the update.", "Update", MessageBoxButton.OK);

			var info = new ProcessStartInfo {
				FileName = "SkyrimTweakerUpdate.exe",
				Arguments = "update " + Process.GetCurrentProcess ().Id.ToString () + " " + file
			};
			var proc = Process.Start (info);
			Application.Current.Shutdown ();
		}

		private void OnUpdateFound (string version) {
			var result = MessageBox.Show (String.Format ("A new version of SkyrimTweaker ({0}) has been found. Would you like to update now?", version),
			                              "Update", MessageBoxButton.YesNo, MessageBoxImage.Question);
			if (result == MessageBoxResult.Yes) {
				var progress = _container.Resolve<UpdateProgressView> ();
				progress.Owner = Application.Current.MainWindow;
				progress.Loaded += (o, e) => _updateService.DownloadUpdate ();
				progress.ShowDialog ();
			}
		}

		#endregion

		#region IShellViewModel Members

		public void Loaded () {
			var region = _regionManager.Regions["MainDock"];
			var view = region.Views.FirstOrDefault ();
			if (view != null) {
				region.Activate (view);
			}

			_updateService.StartUpdateCheck (GetType ().Assembly.GetName ().Version.ToString ());
		}

		#endregion
	}
}
